package com.lgmshare.k3.ui.splash;

import android.Manifest;
import android.content.Intent;

import com.lgmshare.component.logger.Logger;
import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.K3Config;
import com.lgmshare.k3.R;
import com.lgmshare.k3.http.base.SimpleCallback;
import com.lgmshare.k3.http.task.CommonTask;
import com.lgmshare.k3.model.ProductResult;
import com.lgmshare.k3.ui.MainActivity;
import com.lgmshare.k3.ui.base.BaseActivity;

public class SplashActivity extends BaseActivity {

    @Override
    protected boolean showHasActionBar() {
        return false;
    }

    @Override
    protected int onBindLayoutResId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initLoad() {
        CommonTask.GetProductListTask task = new CommonTask.GetProductListTask();
        task.setRequestCallback(new SimpleCallback<ProductResult>() {

            @Override
            public void onStart() {
                super.onStart();
                showProgressDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                dismissProgressDialog();
            }

            @Override
            public void onFailure(int code, String message) {
                Logger.e(TAG, message);
            }

            @Override
            public void onSuccess(ProductResult statusResult) {
                Logger.e(TAG, statusResult.getTotal() + "");
            }
        });
        task.sendPost(this);
        //检查权限
        checkRequestPermission(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE},
                new PermissionCheckCallback() {
                    @Override
                    public void onPermissionsGranted(String[] permissions) {
                        nextPage();
                    }

                    @Override
                    public void onPermissionDenied(String[] permissions) {
                        nextPage();
                    }
                });
    }

    private void nextPage() {
        UIUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(getActivity(), MainActivity.class));
                finish();
            }
        }, K3Config.SPLASH_TIME_DELAY);
    }
}
